package com.itheima.app.controller;

import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MessageManager;
import com.itheima.app.manager.UserManager;
import com.itheima.vo.HuanXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.GET;
import java.util.Map;

/**
 * @author zsk
 * @create 2022-03-21-16:55
 */
@RestController
public class MessageController {

    @Autowired
    private MessageManager messageManager;

    @Autowired
    private UserManager userManager;

    //环信用户信息
    @GetMapping("/huanxin/user")
    public ResponseEntity findHuanxinUser() {
        //获取线程内UserId
        Long userId = UserHolder.get().getId();

        //封装Vo
        HuanXinVo vo = new HuanXinVo();
        vo.setUsername("HX" + userId);
        vo.setPassword("123456");

        //返回数据
        return ResponseEntity.ok(vo);

    }

    //通过环信发送消息
    @PostMapping("/tanhua/strangerQuestions")
    public void strangerQuestions(@RequestBody Map<String, String> param) {
        //接收参数
        long jiarenId = Long.parseLong(param.get("userId"));//佳人id
        String reply = param.get("reply");//回复问题

        //调用manager
        messageManager.strangerQuestions(jiarenId, reply);
    }

    //同意好友申请
    @PostMapping("/messages/contacts")
    public void addContacts(@RequestBody Map<String,Long> param){
        Long friendId = param.get("userId");
        //调用manager
        messageManager.addContacts(friendId);
    }

    //查看好友列表
    @GetMapping("/messages/contacts")
    public ResponseEntity findFriendByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            String keyword){
        //调用manager
        return messageManager.findFriendByPage(pageNum,pageSize,keyword);
    }

    //根据环信id查询用户头像和昵称
    @GetMapping("/messages/userinfo")
    public ResponseEntity findUserInfo(String huanxinId){

        //处理huanxinId
        String userId = huanxinId.replace("HX", "");

        //调用usermanager
        return userManager.findUserInfoVo(Long.parseLong(userId));
    }


    //查看点赞人的信息
    @GetMapping("/messages/likes")
    public ResponseEntity likes(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize
    ){
        //指定类型
        Integer commentType = 1;

        //调用manager
        return messageManager.likes(pageNum,pageSize,commentType);
    }

    //查看评论人消息
    @GetMapping("/messages/comments")
    public ResponseEntity comments(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize
    ){
        //指定类型
        Integer commentType = 2;

        //调用manager
        return messageManager.comments(pageNum,pageSize,commentType);

    }

    //喜欢列表
    @GetMapping("/messages/loves")
    public ResponseEntity loves(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize
    ){
        //确定类型
        Integer commentType = 3;

        //调用manager
        return messageManager.loves(pageNum,pageSize,commentType);
    }

    //查看公告信息
    @GetMapping("/messages/announcements")
    public ResponseEntity announcements(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize
    ){

        //调用manager
        return messageManager.announcements(pageNum,pageSize);

    }


}
